<template>
  <div class="lang">
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        {{ $i18n.locale === 'zh' ? '中文' : 'English'
        }}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="a">中文</el-dropdown-item>
        <el-dropdown-item command="b">English</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
// 导入 cookie
import cookie from 'js-cookie'
export default {
  methods: {
    handleCommand (command) {
      if (command === 'a') {
        // 设置语言状态
        this.$i18n.locale = 'zh'
      } else {
        this.$i18n.locale = 'en'
      }
      // 保存语言到 cookie 中
      cookie.set('locale', this.$i18n.locale)
      // 刷新页面
      this.$router.go(0)
    }
  }
}
</script>

<style lang="scss" scoped>
.lang {
  .el-dropdown {
    margin-right: 10px;
    .el-dropdown-link {
      color: #fff;
    }
  }
}
</style>
